package org.tobaccomain.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.tobaccomain.mapper.EmployeeMapper;
import org.tobaccomain.pojo.dto.PageEmployee;
import org.tobaccomain.pojo.dto.Password;
import org.tobaccomain.pojo.entity.Employee;
import org.tobaccomain.pojo.vo.EmployeeLogin;
import org.tobaccomain.pojo.vo.PageResult;
import org.tobaccomain.service.EmployeeService;
import org.tobaccomain.utils.JwtUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;


//    //添加员工信息
//    @Override
//    public void addEmployee(Employee employee) {
//        employeeMapper.insert(employee);
//    }
//
//    //删除员工信息
//    @Override
//    public void delEmployeeById(Integer id) {
//        employeeMapper.deleteById(id);
//    }
//
//    //修改员工信息
//    @Override
//    public void modifyEmployee(Employee employee) {
//        employeeMapper.updateById(employee);
//    }
//
//    //员工分页查询
//    @Override
//    public PageResult getEmployeeList(PageEmployee pageEmployee) {
//        PageResult pageResult = new PageResult();
//        //查询记录个数
//        Integer total = employeeMapper.count(pageEmployee);
//        pageResult.setTotal(total);
//        //查询记录
//        List<Employee> list = employeeMapper.selectEmployeeList(pageEmployee);
//        pageResult.setRows(list);
//        return pageResult;
//    }
//
//    //根据id获取员工信息
//    @Override
//    public Employee getEmployeeById(Integer id) {
//        return employeeMapper.selectById(id);
//    }

////员工忘记密码
//@Override
//public Employee forgetPassword(Employee employee) {
//    //根据用户输入，判断用户合法性
//    LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<Employee>();
//    wrapper.eq(Employee::getEmployeeId,employee.getEmployeeId());
//    Employee e = employeeMapper.selectOne(wrapper);
//    if(e != null){
//        //用户合法，将其密码改为初始密码
//        Employee newE = new Employee();
//        BeanUtils.copyProperties(e,newE);
//        newE.setPassword("123456");
//        employeeMapper.updateById(newE);
//        return e;
//    }else {
//        //用户不合法
//        return null;
//    }
//}

    //员工登录
    @Override
    public EmployeeLogin login(Employee employee) {
        //查找员工
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getPassword, employee.getPassword())
                .eq(Employee::getEmployeeId, employee.getEmployeeId());
        Employee e = employeeMapper.selectOne(wrapper);
        if(e != null){
            //创建返回对象
            EmployeeLogin employeeLogin = new EmployeeLogin();
            //复制查询到的属性
            BeanUtils.copyProperties(e,employeeLogin);
            //生成jwt令牌
            Map<String,Object> claims = new HashMap<>();
            claims.put("name",e.getName());
            claims.put("password",e.getPassword());
            claims.put("employeeId",e.getEmployeeId());
            String jwt = JwtUtils.generateJwt(claims);
            //将jwt令牌存到返回的对象之中
            employeeLogin.setToken(jwt);
            return employeeLogin;
        }
        return null;
    }

    //员工密码修改
    @Override
    public void modifyPassword(Password password) {
        //查询员工信息
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<Employee>();
        wrapper.eq(Employee::getEmployeeId,password.getEmployeeId());
        Employee employee = employeeMapper.selectOne(wrapper);
        employee.setPassword(password.getNewPwd());
        employeeMapper.updateById(employee);
    }


}
